using AdmissionQA.Application;
using Serilog;
using Serilog.Sinks.PostgreSQL;

var builder = WebApplication.CreateBuilder(args);

// Serilog 配置
var connectionString = builder.Configuration.GetConnectionString("pgsql");
Log.Logger = new LoggerConfiguration()
    .WriteTo.Console()
    .WriteTo.PostgreSQL(
        connectionString: connectionString!,
        tableName: "LogEntries",
        needAutoCreateTable: false,
        columnOptions: new Dictionary<string, ColumnWriterBase>
        {
            { "Id", new SinglePropertyColumnWriter("Id", PropertyWriteMethod.Raw) },
            { "LogLevel", new LevelColumnWriter() },
            { "Message", new RenderedMessageColumnWriter() },
            { "Exception", new ExceptionColumnWriter() },
            { "Timestamp", new TimestampColumnWriter() },
            { "Source", new SinglePropertyColumnWriter("Source") },
            { "UserId", new SinglePropertyColumnWriter("UserId") }
        },
        schemaName: "public"
    )
    .CreateLogger();

builder.Host.UseSerilog();

builder.Services.AddControllers();
builder.Services.AddApplication(builder.Configuration);

// 添加CORS策略，允许前端本地端口跨域访问
builder.Services.AddCors(options =>
{
    options.AddPolicy("AllowLocalhost5173", policy =>
    {
        policy.WithOrigins("http://localhost:5173")
              .AllowAnyHeader()
              .AllowAnyMethod();
    });
});

var app = builder.Build();

// 启用CORS策略
app.UseCors("AllowLocalhost5173");

app.MapControllers();

app.Run();
